package com.example.demo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class ReadTxtFileUtil {
    public static String readTxtFile(String filePath){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            String encoding="utf-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    stringBuilder.append(lineTxt);
                    stringBuilder.append("<br>");
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件"+filePath);
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args){
        System.out.println(ReadTxtFileUtil.readTxtFile("src\\main\\resources\\static\\lyric\\bunengshuodemimi.txt"));
    }
}
